package com.itmayiedu.core.config;

import com.itmayiedu.core.DbConfig.MasterConfig;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * Created by Devin on 2018/1/27.
 * automikos+jta配置多数据源-类1
 */
@Configuration
@MapperScan(basePackages = {"com.itmayiedu.model.test"},sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class MasterDataSource {

    @Primary
    @Bean(name = "test1DataSource")
    public DataSource testDataSource(MasterConfig config) throws SQLException {
        MysqlXADataSource xaDataSource = new MysqlXADataSource();
        xaDataSource.setUrl(config.getUrl());
        xaDataSource.setPinGlobalTxToPhysicalConnection(true);
        xaDataSource.setUser(config.getUsername());
        xaDataSource.setPassword(config.getPassword());
        xaDataSource.setPinGlobalTxToPhysicalConnection(true);

        //AtomikosDataSource 数据源
        AtomikosDataSourceBean atomikosDataSource = new AtomikosDataSourceBean();
        atomikosDataSource.setXaDataSource(xaDataSource);
        atomikosDataSource.setUniqueResourceName("test1DataSource");
        atomikosDataSource.setMinPoolSize(config.getMinPoolSize());
        atomikosDataSource.setMaxPoolSize(config.getMaxPoolSize());
        atomikosDataSource.setMaxLifetime(config.getMaxLifetime());
        atomikosDataSource.setBorrowConnectionTimeout(config.getBorrowConnectionTimeout());
        atomikosDataSource.setLoginTimeout(config.getLoginTimeout());
        atomikosDataSource.setMaintenanceInterval(config.getMaintenanceInterval());
        atomikosDataSource.setMaxIdleTime(config.getMaxIdleTime());
        atomikosDataSource.setTestQuery(config.getTestQuery());

        return atomikosDataSource;
    }

    @Bean(name = "test1SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
//        bean.setMapperLocations(
//                new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/main/*.xml"));
        return bean.getObject();
    }

//    @Bean(name = "test1TransactionManager")
//    @Primary
//    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource){
//        return new DataSourceTransactionManager(dataSource);
//    }

    @Bean("test1SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
